package com.zl.csMall.indexShow.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zl.csMall.code.config.token.TokenTriggerAspect;
import com.zl.csMall.indexShow.vo.MerchantIndexDataVo;
import com.zl.csMall.mine.order.po.Orders;
import com.zl.csMall.mine.order.service.OrderService;
import com.zl.csMall.mine.user.service.UserService;
import com.zl.csMall.product.commodity.service.CommodityService;
import com.zl.csMall.vo.ReturnModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: MerchantIndexController
 * @author: ChenNing
 * @date 2025-10-22 17:30
 */
@CrossOrigin(origins = "*")
@Api(tags = "首页展示 - 商家端")
@RestController
@RequestMapping("csMall/indexShow/merchant")
public class MerchantIndexController {

    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CommodityService commodityService;


    /**
     * @description: 获取商家端首页统计数据
     * @author ChenNing
     * @date: 2025-10-22 17:30
     */
    @ApiOperation(value = "查询商家端首页统计数据 - 商家", response = MerchantIndexDataVo.class)
    @TokenTriggerAspect
    @CrossOrigin("*")
    @GetMapping
    public ReturnModel getIndexData(@RequestHeader(required = false) String token){
        // 访客数
        Long visitorQuantity = userService.getBaseMapper().selectCount(new QueryWrapper<>());
        // 订单数
        Long orderQuantity = orderService.getBaseMapper().selectCount(new QueryWrapper<>());
        // 总商品数
        Long commodityQuantity = commodityService.getBaseMapper().selectCount(new QueryWrapper<>());
        // 待发货数
        LambdaQueryWrapper<Orders> dfhQuery = new LambdaQueryWrapper<>();
        dfhQuery.eq(Orders::getStatus, 2);
        Long toBeDispatchedQuantity = orderService.getBaseMapper().selectCount(dfhQuery);
        // 待收货数
        LambdaQueryWrapper<Orders> dshQuery = new LambdaQueryWrapper<>();
        dshQuery.eq(Orders::getStatus, 3);
        Long pendingReceiptQuantity = orderService.getBaseMapper().selectCount(dshQuery);
        // 已完成数
        LambdaQueryWrapper<Orders> ywcQuery = new LambdaQueryWrapper<>();
        ywcQuery.eq(Orders::getStatus, 4);
        Long completedQuantity = orderService.getBaseMapper().selectCount(ywcQuery);

        MerchantIndexDataVo dataVo = new MerchantIndexDataVo(visitorQuantity, orderQuantity, commodityQuantity, toBeDispatchedQuantity, pendingReceiptQuantity, completedQuantity);
        return new ReturnModel(dataVo);
    }

}
